package com.bringway.p2p.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.global.controller.JsonController;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.global.domain.TreeNode;
import com.bringway.global.util.RequestUtils;
import com.bringway.p2p.service.AppRootService;
import com.bringway.p2p.service.P2PUserService;

import net.sf.json.JSONObject;

/*
 * @author qjy
 * @className MyUserController
 * @description 用户操作控制器
 * @date 2015年12月17日下午3:53:51
 */
@Controller
@RequestMapping("/p2puser")
public class P2PUserController extends JsonController {
	@Autowired
	private P2PUserService userService;
	private List users = null;

	/**
	 * @方法名称:userList
	 * @方法描述:查询用户列表---tree
	 * @author qjy
	 * @date 2015年12月17日下午3:55:00
	 * @return
	 */
	@RequestMapping("/queryUserList")
	public ModelAndView userList01(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject parms = new JSONObject();
		users = userService.users(parms);
		Iterator iterator = users.iterator();
		ArrayList nodeList = new ArrayList();
		while (iterator.hasNext()) {
			Map map = (Map) iterator.next();
			iterator.remove();
			TreeNode t = new TreeNode();
			t.setAsync(true);
			t.setText(map.get("GYH").toString());
			t.setId(map.get("ID").toString());
			nodeList.add(t);
		}
		serialize(request, response, nodeList, true);
		return null;
	}

	/**
	 * @方法名称:userList02
	 * @方法描述:查询用户列表---grid 用到分页
	 * @author qjy
	 * @date 2015年12月28日下午2:37:27
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryUserListAll")
	public ModelAndView userList02(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		String XM = RequestUtils.getStringParameter(request, "XM");
		String GWMC = RequestUtils.getStringParameter(request, "GWMC");
		String GYH = RequestUtils.getStringParameter(request, "GYH");
		String NAME = RequestUtils.getStringParameter(request, "NAME");
		String ROLE = RequestUtils.getStringParameter(request, "ROLE_ID");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		JSONObject parms = new JSONObject();
		JSONObject result = new JSONObject();
		parms.put("XM",XM);
		parms.put("GWMC",GWMC);
		parms.put("GYH", GYH);
		parms.put("NAME", NAME);
		parms.put("ROLE", ROLE);
		parms.put("start", start);
		parms.put("limit", limit);
		users = userService.usersAllLimit(parms);
		int total = userService.getUsersItems(parms);
		result.put("data", users);
		result.put("total", total);
		if (null!=page) {//新版本,传参
			int totalPage=total%limit==0?total/limit:total/limit+1;
			result.put("totalPage", totalPage);
		}
		serialize(request, response, result);
		return null;
	}

	/**
	 * @方法名称:addUser
	 * @方法描述:添加用户
	 * @author qjy
	 * @date 2015年12月28日下午4:28:11
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addUser")
	public ModelAndView addUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String GYH = RequestUtils.getStringParameter(request, "GYH");
		String SJHM = RequestUtils.getStringParameter(request, "SJHM");
		String USER_PASSWORD = RequestUtils.getStringParameter(request, "USER_PASSWORD");
		String XM = RequestUtils.getStringParameter(request, "XM");
		String GWMC = RequestUtils.getStringParameter(request, "GWMC");
		String ROLEID = RequestUtils.getStringParameter(request, "ROLEID");
		JSONObject parms = new JSONObject();
		JSONObject result = new JSONObject();
		String id = UUID.randomUUID().toString();
		parms.put("ID", id);
		parms.put("GYH", GYH);
		parms.put("SJHM", SJHM);
		parms.put("USER_PASSWORD", USER_PASSWORD);
		parms.put("XM", XM);
		parms.put("GWMC", GWMC);
		parms.put("ROLEID", ROLEID);
		boolean bool=true;
		try {
			bool = userService.addUser(parms);
			result.put("isAdd", bool);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("isAdd", false);
		}
		// 分配一个默认的角色--普通员工
		// userService.addUserRole(id,"856fd3fe-69e2-474a-b8d7-7f96bfbea9b5",XM);
		result.put("success", true);
		serialize(request, response, result);
		return null;
	}

	/**
	 * @方法名称:deleteUser
	 * @方法描述:删除用户的方法
	 * @author qjy
	 * @date 2015年12月29日上午9:31:02
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deleteUser")
	public ModelAndView deleteUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = RequestUtils.getStringParameter(request, "id");
		JSONObject parms = new JSONObject();
		parms.put("id", id);
		boolean bool = userService.deleteUser(parms);
		JSONObject result = new JSONObject();
		result.put("success", true);
		result.put("isDelete", bool);
		serialize(request, response, result);
		return null;
	}

	/**
	 * @方法名称:updateUser
	 * @方法描述:更新用户信息
	 * @author qjy
	 * @date 2015年12月29日下午1:55:30
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateUser")
	public ModelAndView updateUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = RequestUtils.getStringParameter(request, "id");
		String xm = RequestUtils.getStringParameter(request, "xm");
		String gwmc = RequestUtils.getStringParameter(request, "gwmc");
		String role = RequestUtils.getStringParameter(request, "role");
		JSONObject parms = new JSONObject();
		parms.put("id", id);
		parms.put("xm", xm);
		parms.put("gwmc", gwmc);
		parms.put("role", role);
		boolean bool = userService.updateUser(parms);
		JSONObject result = new JSONObject();
		result.put("success", true);
		result.put("isUpdate", bool);
		serialize(request, response, result);
		return null;
	}
	/**
	 * @方法名称:updateappUser
	 * @方法描述:更新用户信息
	 * @author qjy
	 * @date 2015年12月29日下午1:55:30
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Autowired
	private AppRootService appRootService;
	@RequestMapping("/updateAppUser")
	public ModelAndView updateTjr(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

		String yhm = RequestUtils.getStringParameter(request, "yhm");
		String xm = RequestUtils.getStringParameter(request, "xm");
		String gwmc = RequestUtils.getStringParameter(request, "gwmc");
		String role = RequestUtils.getStringParameter(request, "role");
		JSONObject parms = new JSONObject();
		parms.put("yhm", yhm);
		parms.put("xm", xm);
		parms.put("gwmc", gwmc);
		parms.put("role", role);
		boolean bool=false;
		parms.put("username", yhm);
		boolean a=appRootService.queryGyh(parms);
		if (!a) {
			bool = userService.updateAppUser(parms);
		}
		JSONObject result = new JSONObject();
		result.put("success", true);
		result.put("isUpdate", bool);
		serialize(request, response, result);
		return null;
	}

	/**
	 * @方法名称:resetPassword
	 * @方法描述:重置密码
	 * @author qjy
	 * @date 2015年12月29日下午4:27:57
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/resetPassword")
	public ModelAndView resetPassword(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = RequestUtils.getStringParameter(request, "id");
		JSONObject parms = new JSONObject();
		parms.put("id", id);
		boolean bool = userService.resetPassword(parms);
		JSONObject result = new JSONObject();
		result.put("success", true);
		result.put("isUpdate", bool);
		serialize(request, response, result);
		return null;
	}

	/**
	 * @方法名称: changePassword
	 * @方法描述: 修改密码的操作
	 * @时间:2016年2月18日下午5:11:36
	 * @author:qjy
	 * @param reqeust
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/changePassword")
	public ModelAndView changePassword(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String oldPassword = RequestUtils.getStringParameter(request, "oldPassword");
		String gyh = RequestUtils.getStringParameter(request, "gyh");
		String confirmPassword = RequestUtils.getStringParameter(request,"confirmPassword");
		String newPassword = RequestUtils.getStringParameter(request, "USER_PASSWORD");
		JSONObject parms = new JSONObject();
		JSONObject result = new JSONObject();
		if(confirmPassword.equals(newPassword)){
			
			parms.put("gyh", gyh);
			parms.put("oldPassword", oldPassword);
			boolean bool = userService.comparePassword(parms);
			if (bool) {
				// 如果旧密码输入正确那么进行下一步操作
				parms.clear();
				
				parms.put("GYH", gyh);
				parms.put("newPassword", newPassword);
				bool = userService.changePassword(parms);
				result.put("success", true);
				result.put("msg", "修改密码成功！");
				result.put("isSuccess", bool);
			} else {
				// 如果旧密码输入不正确，那么返回信息
				result.put("success", true);
				result.put("msg", "原密码输入错误！！");
			}
			serialize(request, response, result);
		}else{
			result.put("success",true);
			result.put("msg","两次输入的密码不一样！");
			serialize(request,response,result);
		}
		return null;
	}

	/**
	 * @方法名称: p2pLogin
	 * @方法描述: 登陆验证
	 * @时间:2016年2月29日上午10:40:00
	 * @author:qjy
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/p2pLogin")
	public ModelAndView p2pLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String username = RequestUtils.getStringParameter(request, "j_username");
		String password = RequestUtils.getStringParameter(request, "j_password");
		JSONObject params = new JSONObject();
		params.put("username", username);
		params.put("password", password);
		Map<String,Object> loginInfos = userService.login(params);
			JSONObject result = new JSONObject();
			result.put("data",loginInfos);
			serialize(request, response, result);
			return null;
	}
	
	
	
	
}
